package com.taijihuabao.common.commonutils;

import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author changhui.tu
 * 生成验证码相关工具类
 */
public class RandomValidateCodeUtil {

    /**
     * 随机产生数字与字母组合的字符串
     */
    private static final String RAND_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    /**
     * 图片宽、高，干扰线数量，随机产生字符数量(循环次数)
     * random.nextInt():会随机生成一个整数，这个整数的范围就是int类型的范围-2^31 ~ 2^31-1,但是如果在nextInt()括号中加入一个整数a那么，这个随机生成的随机数范围就变成[0,a)。
     */
    private static final int WIDTH = 112;
    private static final int HEIGHT = 31;
    private static final int LINE_SIZE = 40;
    private static final int STRING_NUM = 4;
    private final static Random RANDOM = new Random();
    private static String finalCode = null;

    /**
     * 生成六位数字随机验证码
     *
     * @return
     */
    public static int sixMathValidateCode() {
        return (int) ((Math.random() * 9 + 1) * 100000);
    }

    /**
     * 生成四位数字随机验证码
     *
     * @return
     */
    public static int fourMathValidateCode() {
        return (int) ((Math.random() * 9 + 1) * 1000);
    }

    /**
     * 生成随机图片
     */
    public static void getRandcode(HttpServletResponse response) {
        // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
        // 产生Image对象的Graphics对象,该对象可以在图像上进行各种绘制操作
        Graphics graphics = image.getGraphics();
        //图片大小
        graphics.fillRect(0, 0, WIDTH, HEIGHT);
        //字体大小
        graphics.setFont(new Font("Times New Roman", Font.PLAIN, 18));
        //字体颜色
        graphics.setColor(getRandColor(110, 133));
        // 绘制干扰线
        for (int i = 0; i <= LINE_SIZE; i++) {
            drowLine(graphics);
        }
        // 绘制随机字符
        String randomString = "";
        for (int i = 1; i <= STRING_NUM; i++) {
            randomString = drowString(graphics, randomString, i);
        }
        finalCode = randomString;
        //处理并释放资源
        graphics.dispose();
        try {
            // 将内存中的图片通过流动形式输出到客户端
            ImageIO.write(image, "JPEG", response.getOutputStream());
        } catch (Exception e) {
            throw new ServiceException(ApplicationResponseCode.INTER_ERROR);
        }
    }

    /**
     * 获得字体
     */
    private static Font getFont() {
        return new Font("Fixedsys", Font.BOLD, 18);
    }

    /**
     * 获得颜色
     */
    private static Color getRandColor(int fc, int bc) {
        int maxMath = 255;
        if (fc > maxMath) {
            fc = 255;
        }
        if (bc > maxMath) {
            bc = 255;
        }
        int colorR = fc + RANDOM.nextInt(bc - fc - 16);
        int colorG = fc + RANDOM.nextInt(bc - fc - 14);
        int colorB = fc + RANDOM.nextInt(bc - fc - 18);
        return new Color(colorR, colorG, colorB);
    }

    /**
     * 绘制干扰线
     */
    private static void drowLine(Graphics g) {
        int x = RANDOM.nextInt(WIDTH);
        int y = RANDOM.nextInt(HEIGHT);
        int xl = RANDOM.nextInt(13);
        int yl = RANDOM.nextInt(15);
        g.drawLine(x, y, x + xl, y + yl);
    }

    /**
     * 绘制字符串
     */
    private static String drowString(Graphics graphics, String randomString, int xTrans) {
        //设置字段、颜色
        graphics.setFont(getFont());
        graphics.setColor(new Color(RANDOM.nextInt(101), RANDOM.nextInt(111), RANDOM.nextInt(121)));
        //拿取定义的字段的随机一个数值(用户绘图)
        String rand = String.valueOf(RAND_STRING.charAt(RANDOM.nextInt(RAND_STRING.length())));
        randomString += rand;
        //坐标
        graphics.translate(RANDOM.nextInt(3), RANDOM.nextInt(3));
        //使用图形上下文的当前字体和颜色绘制指定字符串所给出的文本。最左边字符的基线位于这个图形上下文的坐标系统中的位置(x，u)。
        graphics.drawString(rand, 13 * xTrans, 16);
        return randomString;
    }

    /**
     * 获取最终生成的字符验证码
     *
     * @return
     */
    public static String getFinalCode() {
        return finalCode;
    }

}
